package dk.hewison.util;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-09 12:39:39 +0000 (Mon, 09 Feb 2009) $:
 * @version $Revision: 363 $:
 */
public class FolderScanner {


    private FolderInfo rootFolder;

    private int imageCount = 0;

    public FolderScanner(String folder) throws FileNotFoundException {

        long start = System.currentTimeMillis();

        scan(null, new File(folder));

        long time = System.currentTimeMillis() - start;
        Log.debug(imageCount + " thumbnails took " + (time) + " ms");


        Log.debug("images found " + imageCount);

        start = System.currentTimeMillis();

        writeFolderInfoToDisk(rootFolder);

        /*   StringBuffer buf = new StringBuffer();

    try {
        *//**
         * 9 thumbnails took 3778 ms 100x100
         avg 419 ms per thumbnail

         9 thumbnails took 4423 ms 600x600
         avg 491 ms per thumbnail

         9 thumbnails took 7740 ms both
         avg 860 ms per thumbnail

         9 thumbnails took 5791 ms both JAVA6
         avg 643 ms per thumbnail
         *//*
                    //HxImageIO.createThumbnail(new File(dirFile, imageFilename));

                    *//**
         * 9 thumbnails took 5612 ms 100x100
         avg 623 ms per thumbnail

         9 thumbnails took 6200 ms 600x600
         avg 688 ms per thumbnail
         *//*
                    *//*FileOutputStream fos = new FileOutputStream(thumbnailFile);
                Thumbnail.createThumb(getBytesFromFile(imageFile), 600, 600, 2, fos);*//*

                    //Log.debug("created thumbnail " + thumbnailFile.getAbsolutePath());
                } catch (Exception e) {
                    e.printStackTrace();
                }*/

        /*
                    File dirInfoFile = new File(dirFile, "images.txt");
                    FileOutputStream fos = new FileOutputStream(dirInfoFile);
                    try {
                        fos.write(buf.toString().getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
        */

        time = System.currentTimeMillis() - start;
        Log.debug(imageCount + " thumbnails took " + (time) + " ms");
        Log.debug("avg " + (time / imageCount) + " ms per thumbnail");
    }


    private void writeFolderInfoToDisk(FolderInfo folderInfo) {

        File imagesInfoFile = new File(folderInfo.getDirectoryFile(), ".images.txt");
        File dirInfoFile = new File(folderInfo.getDirectoryFile(), ".dir.json");

        //todo make these files hidden on windows
        try {
            FileOutputStream fos = new FileOutputStream(imagesInfoFile);
            fos.write(folderInfo.getImageNamesString().getBytes());
            fos.close();

            fos = new FileOutputStream(dirInfoFile);
            fos.write(folderInfo.getSubFoldersString().getBytes());
            fos.close();

            Log.debug(folderInfo.getSubFoldersString());
            Log.debug(folderInfo.getImageNamesString());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        //create thumbnails
        for (String imageFilename : folderInfo.getImageFilenames()) {
            try {
                //HxImageIO.createThumbnail(new File(folderInfo.getDirectoryFile(), imageFilename));
                ThumbnailCreater2.createThumbnail(new File(folderInfo.getDirectoryFile(), imageFilename));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        for (FolderInfo info : folderInfo.getFolderInfos()) {
            writeFolderInfoToDisk(info);
        }

    }


    private FolderInfo scan(FolderInfo parentFolderInfo, File directoryFile) {

        FolderInfo folderInfo = new FolderInfo(parentFolderInfo, directoryFile);

        if (parentFolderInfo == null) {
            rootFolder = folderInfo;
        } else {
            parentFolderInfo.addFolderInfo(folderInfo);
        }

        File[] files = directoryFile.listFiles();

        for (File file : files) {
            //Log.debug(file.getAbsolutePath());
            if (file.isHidden()) continue;

            if (file.isDirectory()) {
                scan(folderInfo, file);
            } else if (isImage(file)) {
                folderInfo.addImageFilename(file.getName());
                imageCount++;
            }
        }
        return folderInfo;

    }

    private boolean isImage(File file) {
        String filename = file.getName();
        return filename.endsWith("JPG") || filename.endsWith("jpg");
    }

    /**
     * Start this with
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            if (args == null || args.length == 0) {
                new FolderScanner("./");
            } else {
                new FolderScanner(args[0]);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
